<?php
class Business_Customer{
	/**
	 * 
	 * @return Array | Zend_Exception
	 */
	public static function getAllId(){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_getAllCustomerById()');
			$stmt->execute();
			$result = $stmt->fetchAll();
			return $result;
		} catch (Zend_Exception $e) {
			
			return array();
		}
	}
	
	public static function getAllIds($ids){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_getCustomerByIds(:p_ids)');
			$stmt->bindValue('p_ids',$ids);
			$stmt->execute();
			$result = $stmt->fetchAll();

			return $result;
		} catch (Zend_Exception $e) {
			return $e->getMessage();
		}
	}
	
	/**
	 * 
	 * @param $params['key'] = CustomerID,CustomerName | $params['value'] = 1, Name
	 * @return Array
	 */
	public static function get($params){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_getCustomerBy'.$params['key'].'(:s_'.$params['key'].')');
			$stmt->bindValue('s_'.$params['key'],$params['value']);
			$stmt->execute();
			$result = $stmt->fetchAll();
			unset($stmt);
			
			return $result;
		} catch (Zend_Exception $e) {
			return array();
		}
	}
	

	public static function create($params){
		try {
			
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_insertCustomer(:s_CustomerName,
:s_Address
,:s_Tel
,:s_Mobile
,:s_Email
,:s_Fax
,:s_CustometType
,:s_Logo
,:s_Contact
,:s_Website)');
			$stmt->bindValue('s_CustomerName',$params['CustomerName']);
			$stmt->bindValue('s_Address',$params['Address']);
			$stmt->bindValue('s_Tel',$params['Tel']);
			$stmt->bindValue('s_Mobile',$params['Mobile']);
			$stmt->bindValue('s_Email',$params['Email']);
			$stmt->bindValue('s_Fax',$params['Fax']);
			$stmt->bindValue('s_CustometType',$params['CustomerType']);
			$stmt->bindValue('s_Logo',$params['logo']);
			$stmt->bindValue('s_Contact',$params['Contact']);
			$stmt->bindValue('s_Website',$params['Website']);
			$stmt->execute();
			$result = $stmt->fetch();
			

			
			return $result['result'];
		} catch (Zend_Exception $e) {
			return 0;
		}
	}
	/**
	 * 
	 * @param unknown_type $params
	 * @return number
	 */
	public static function update($params){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare('CALL sp_updateCustomerbyId(
			:s_CustomerId,:s_CustomerName,
:s_Address
,:s_Tel
,:s_Mobile
,:s_Email
,:s_Fax
,:s_CustometType
,:s_Logo
,:s_Contact
,:s_Website)');
			$stmt->bindValue('s_CustomerId',$params['CustomerId']);
			$stmt->bindValue('s_CustomerName',$params['CustomerName']);
			$stmt->bindValue('s_Address',$params['Address']);
			$stmt->bindValue('s_Tel',$params['Tel']);
			$stmt->bindValue('s_Mobile',$params['Mobile']);
			$stmt->bindValue('s_Email',$params['Email']);
			$stmt->bindValue('s_Fax',$params['Fax']);
			$stmt->bindValue('s_CustometType',$params['CustomerType']);
			$stmt->bindValue('s_Logo',$params['logo']);
			$stmt->bindValue('s_Contact',$params['Contact']);
			$stmt->bindValue('s_Website',$params['Website']);
			$stmt->execute();			
			
			$result = $stmt->rowCount();
			

			
			return $result;
		} catch (Zend_Exception $e) {
			return $e->getMessage();
		}
	}
	
	public static function delete($CustomerId){
		try {
			$db = Core_Global::getDatabase();
			$stmt = $db->prepare("CALL sp_deleteCustomerbyIds(:s_CustomerId)");
			$stmt->bindValue('s_CustomerId',$CustomerId);
			$stmt->execute();
			return $stmt->rowCount();
		} catch (Zend_Exception $e) {
			return $e->getMessage();
		}
	}
}